package com.maoye.ossp.biz.brand.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.dao.ISupplierShopBrandDao;
import com.maoye.ossp.biz.brand.entity.SupplierShopAccount;
import com.maoye.ossp.biz.brand.service.ISupplierBrandService;
import com.maoye.ossp.biz.brand.service.ISupplierShopAccountService;
import com.maoye.ossp.biz.brand.service.ISupplierShopBrandService;
import com.maoye.ossp.common.util.RequestUtil;
import com.maoye.ossp.common.web.BaseController;



/**
 * 对象功能:子供应商基本信息 控制器类 开发公司:maoye shenzhen 开发人员:huj 创建时间:2013-09-30 10:04:52
 */
@Controller
@RequestMapping("/biz/brand/supplierShopAccount/")
public class SupplierShopAccountController extends BaseController {
	@Resource
	private ISupplierShopAccountService supplierShopAccountService;
	@Resource
	private ISupplierShopBrandDao supplierShopBrandDao;
	@Resource
	private ISupplierBrandService supplierBrandService;
	@Resource
	private ISupplierShopBrandService supplierShopBrandService;
	
	/**
	 * 取得子供应商基本信息分页列表
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("list")
//	@Action(description = "查看子供应商基本信息分页列表")
	public String list(SupplierShopAccount supplierShopAccount,HttpServletRequest request,
			HttpServletResponse response,Model model) throws Exception {
		// List<Supplier> list=supplierService.getAll(new
		// QueryFilter(request,"supplierItem"));
//		QueryFilter queryFilter = new QueryFilter(request, "supplierShopItem");
		List<SupplierShopAccount> list = supplierShopAccountService.getAll();
		model.addAttribute("supplierShopList", list);
		return resultPage("/biz/brand/supplierShopAccount/list", model, request);
	}

	/**
	 * 	编辑供应商品牌关系信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "{id}/edit")
//	@Action(description="编辑供应商品牌关系信息")
	public String edit(@PathVariable String id,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception
	{
//		Long supplierId=Long.valueOf(id);
//		
//		String returnUrl=RequestUtil.getPrePage(request);
//		//获得当前供应商信息
//		SupplierShopAccount supplier=supplierShopAccountService.get(supplierId);
//		List<SupplierBrand> supplierBrandList=supplierBrandService.getSupplierShopBrandList(supplierId);
//		model.addAttribute("supplier",supplier);
//		model.addAttribute("supplierBrandList",supplierBrandList);
//		model.addAttribute("returnUrl",returnUrl);
//		model.addAttribute("actionEnum");
		return "/biz/brand/supplierShopAccount/edit";
	}
	/**
	 * 	编辑供应商品牌关系信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "{id}/view")
//	@Action(description="查看供应商品牌关系信息")
	public String get(@PathVariable String id,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception
	{
//		
//		//获得当前供应商信息
//		SupplierShopAccount supplier=supplierShopAccountService.get(Long.valueOf(id));
//		List<SupplierBrand> supplierBrandList=supplierBrandService.getSupplierShopBrandList(Long.valueOf(id));
//		model.addAttribute("supplier",supplier);
//		model.addAttribute("supplierBrandList",supplierBrandList);
		return "/biz/brand/supplierShopAccount/";
	}
	
	
	/**
	 * 	编辑供应商品牌关系信息
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("save")
//	@Action(description="编辑供应商品牌关系信息")
	public void save(HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) throws Exception
	{
		String resultMsg=null;	
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher((new String[] { "yyyy-MM-dd" })));
		String json=RequestUtil.getString(request, "json");
		JSONObject obj = JSONObject.fromObject(json);
		try{
			supplierShopBrandService.updateAllBrand(obj);
			resultMsg="添加品牌成功";
//			writeResultMessage(response.getWriter(),resultMsg,ResultMessage.Success);
			addMessage(redirectAttributes, "添加成功！");
		}catch(Exception e){
			e.printStackTrace();
//			writeResultMessage(response.getWriter(),resultMsg+","+e.getMessage(),ResultMessage.Fail);
			addMessage(redirectAttributes, "保存失败！"+e.getMessage());
		}
	}
	

}
